<?php $this->widget('application.widgets.jui.initial');?>
<?php
            $this->widget('application.widgets.listicon.ListIcon', array(
                    'icons'=>array(
                        array('value'=>'Add new packages','icon'=>'ui-icon-plus','needtext'=>"true"),
                        array('value'=>'View report','icon'=>'ui-icon-document','needtext'=>"true",'htmlOptions'=>array('href'=>CHtml::normalizeUrl(array('packing_list/report','id'=>$model->id)))),
                    ),
                    'htmlOptions'=>array('style'=>'text-align: right;','name'=>'packing_list_icon')
            ));
        ?>
<div class="view">
<fieldset id="packing_list" class="ui-widget section">
        <legend class="ui-widget-header ui-corner-all">Shipment information</legend>
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
                'date',
		array('value'=>$model->shipping_line()->cname,'type'=>'text','label'=>'Shipping agent'),
		array('value'=>CHtml::link($model->port()->cname,CHtml::normalizeUrl(array('company/view','id'=>$model->port()->id))),'type'=>'raw','label'=>'Port of discharge'),
                'subject',
	),
)); ?>
</fieldset>
<fieldset id="packing_list_package" class="ui-widget section">
        <legend class="ui-widget-header ui-corner-all">Shipment Packages</legend>
        
	<?php 
		$t = count($model->packages());
        foreach($model->packages() as $g=>$package) {
            echo "<div id='packages_container'>";
            $this->renderPartial('_viewpackingform',array('package'=>$package));
            echo "</div>";
            if($g<$t-1)
                echo "<hr>";
        }
        ?>
</fieldset>
</div>
<?php
$cs = Yii::app()->getClientScript();
$viewPackageURL = CHtml::normalizeUrl(array('packing_list/viewpackage','packing_list_id'=>$model->id));
$NewPackageURL = CHtml::normalizeUrl(array('packing_list/NewPackage','packing_list_id'=>$model->id));
$s =<<<EOD
$('div [name="packing_list_icon"]>a:eq(0)').live('click',function(){
    $('fieldset#packing_list_package').append('<hr /><div id="packages_container"></div>');
    var container = $('fieldset#packing_list_package>#packages_container:last');
    $(container).subcontent({
        url:'$NewPackageURL',
        afterShow:function(){
            $(container).find('input[type="submit"]').unbind();
            $(container).find('input[type="submit"]').bind({'click':function(){
                    var newSubmit = $.extend({},$.simplelogform,{afterSubmit:function(data){
                                            $(container).subcontent({
                                                url:'$viewPackageURL?id='+data.id
                                            });
                                        }
                                    });
                    var i=0;
                    var cargo_id = [];
                    $(container).find('#package_do_list').find('input').each(function(){
                        var name = "cargo["+i+"][id]";
                        var value = $(this).attr('cargo_id');
                        cargo_id[name] = value;
                        i++;
                    });
                    newSubmit.Submit($(container).find('form'),cargo_id);
                    return false;
                }
            });
            $(container).find('input#Cancel').click(function(){
                $('fieldset#packing_list_package>#packages_container:last, fieldset#packing_list_package>hr:last').remove();
            });
        }
    });
    return false;
});
EOD;
$cs->registerScript('packing_list#view',$s);
?>